<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="javax.persistence.TypedQuery"%>
<%@page import="rhserver.MainController"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="Entities.Resourcecategory"%>
<%@page import="Entities.Resource"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Resource Categories</title>
    </head>
    <body>

        <table width="80%" border="1" cellspacing="2" cellpadding="2" align="center">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Category</th>
                    <th>Telephone Number</th>
                </tr>
            </thead>
            <tbody>

                <%
                    List<Resource> resources = (List<Resource>) request.getAttribute("resources");
                    List<Resourcecategory> categories = (List<Resourcecategory>) request.getAttribute("categories");
                    for (Resource resource : resources) {%>
                <tr>
                    <td align="center"> <%= resource.getId()%> </td> 
                    <td align="center"> <%= resource.getName()%> </td>
                    <td align="center"> <%= resource.getDescription()%> </td>
                    <td align="center"> <%= resource.getCategory().getName()%> </td>
                    <td align="center"> <%= resource.getTelnumber()%> </td>
                    <td align="center"> <form method="POST" action="ResourceServlet">
                            <input type="hidden" name="id" value="<%= resource.getId()%>">
                            <input type="submit" value="Delete" /></form>
                </tr> 
                <% }%>
            </tbody>
        </table>
        <hr size="5px" />
        <h2>Add a new resource:</h2>
        <form method="POST" action="ResourceServlet">
            Name: <input type="text" name="name" /><br/>
            Description: <input type="text" name="description" /><br/>
            Category:<br/>
            <select name="categoryid">
                <%
                    boolean firstelement = true;
                    for (Resourcecategory category : categories) {%>
                <option <%
                    if (firstelement) {%> selected="selected" <% firstelement = false;
                   }%> 
                    value="<%=category.getId()%>"><%=category.getName()%>
                </option>
                <% }%>
            </select><br/>
            Telephone Number <input type="text" name="telnumber" /><br/>
            <input type="submit" value="Add" />
        </form>
    </body>
</html>
